package com.yyds.yygh.hospclient.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyds.yygh.common.result.Result;
import com.yyds.yygh.common.utils.MD5;
import com.yyds.yygh.hospclient.service.HospitalSetService;
import com.yyds.yygh.model.hosp.HospitalSet;
import com.yyds.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping(value = "/admin/hosp/hospitalSet")
public class HospitalSetController {
    //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    //1 查询医院设置表所有信息
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping(value = "/findAll")
    public Result findAllHospitalSet(){
        //调用service方法
        List<HospitalSet> list = hospitalSetService.list();
        return Result.success(list);
    }

    //2逻辑删除医院设置
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping(value = "{id}")
    public Result deleteById(@ApiParam("医院id") @PathVariable("id") Long id){
        boolean bool = hospitalSetService.removeById(id);
        if (bool){
            return Result.success();
        }
        return Result.fail();
    }
    //3带条件的分页查询
    @ApiOperation(value = "带条件的分页查询")
    @PostMapping(value = "/findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable("current") Integer current,
                                  @PathVariable("limit") Integer limit,
                      @RequestBody(required = false)HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> page = new Page(current,limit);
        //条件构造器
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if (hospitalSetQueryVo.getHoscode()!=null && !"".equals(hospitalSetQueryVo.getHoscode())){
            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }
        if(hospitalSetQueryVo.getHosname()!=null){
            wrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        Page<HospitalSet> pages = hospitalSetService.page(page, wrapper);
        return Result.success(pages);
    }
    //4添加医院设置
    @ApiOperation(value = "添加医院设置")
    @PostMapping(value = "/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置医院状态(0代表不可用 1代表可用)
        hospitalSet.setStatus(1);
        //获取随机数并且加密
        Random random = new Random();
        String src = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000));
        //设置医院签名秘钥
        hospitalSet.setSignKey(src);
        boolean bool = hospitalSetService.save(hospitalSet);
        if (bool){
            return Result.success();
        }
        return Result.fail();
    }
    //5根据id获取医院设置
    @ApiOperation(value = "根据id获取医院设置")
    @GetMapping(value = "/getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        HospitalSet one = hospitalSetService.getById(id);
        return Result.success(one);
    }
    //6 修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PutMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean bool = hospitalSetService.saveOrUpdate(hospitalSet);
        if (bool){
            return Result.success();
        }
        return Result.fail();
    }
    //7 批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping(value = "/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        boolean bool = hospitalSetService.removeByIds(idList);
        if (bool){
            return Result.success();
        }
        return Result.fail();
    }
    //8 医院设置锁定与解锁
    @ApiOperation(value = "医院设置锁定与解锁")
    @PutMapping(value = "/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id , @PathVariable Integer status){
        /*获取医院id*/
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //修改状态码
        hospitalSet.setStatus(status);
        //更新数据库
        hospitalSetService.updateById(hospitalSet);
        return Result.success();
    }
    //9 发送签名秘钥
    @ApiOperation(value = "发送签名秘钥")
    @PutMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hosp = hospitalSetService.getById(id);
        String hoscod = hosp.getHoscode();
        String key = hosp.getSignKey();
        //TUDO 发送短信(未完成,待办事项)
        return Result.success();
    }

}
